package com.example.a22310.myapplication.ViewModel.User;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Activity.AttentionActivity;
import com.example.a22310.myapplication.Activity.MyAddress;
import com.example.a22310.myapplication.Activity.OrderActivity;
import com.example.a22310.myapplication.Activity.PrizeActivity;
import com.example.a22310.myapplication.Activity.UserPost;
import com.example.a22310.myapplication.Beans.BubbleNumber;
import com.example.a22310.myapplication.Listener.ClubListener;
import com.example.a22310.myapplication.Model.UserModel;

public class UserInformationViewModel implements ClubListener<BubbleNumber> {
    private ActivityUserInformationBinding binding;
    private AppCompatActivity activity;
    private int userid;
    int REQUEST_CODE = 1;
    private boolean flag_post,flag_attention,flag_prize,flag_order;



    public UserInformationViewModel(AppCompatActivity activity, ActivityUserInformationBinding binding, int userid){
        this.activity = activity;
        this.binding = binding;
        this.userid = userid;
        binding.setModel(this);
        UserModel userModel=new UserModel();
        userModel.bubbleNum(UserInformationViewModel.this,userid);

    }


    public void post(View view) {
        if (flag_post == true){
            Intent intent = new Intent();
            intent.setClass(activity, UserPost.class);
            activity.startActivity(intent);
        }else{
            disPlay("没有帖子？去给自己抢个沙发呗！");
        }
    }

    public void address(View view) {
        Intent intent = new Intent();
        intent.setClass(activity, MyAddress.class);
        activity.startActivityForResult(intent, REQUEST_CODE);
    }

    public void order(View view) {
        if (flag_order == true){
            Intent intent = new Intent();
            intent.setClass(activity, OrderActivity.class);
            activity.startActivity(intent);
        }else {
            disPlay("订单都没有，点什么。还不快去下单");
        }

    }
    public void back(View view) {
        activity.onBackPressed();
    }
    public void prizelist(View view) {
        if (flag_prize == true){
            Intent intent = new Intent();
            intent.setClass(activity, PrizeActivity.class);
            activity.startActivity(intent);
        }else{
            disPlay("运气真背，一个奖品都没有。");
        }

    }
    public void attention(View view) {
        if (flag_attention == true) {
            Intent intent = new Intent();
            intent.setClass(activity, AttentionActivity.class);
            activity.startActivity(intent);
        }else{
            disPlay("关注列表空荡荡的，好冷清，快去添加关注吧，孩子!");
        }
    }

    @Override
    public void onSuccess(BubbleNumber bubbleNumber) {
        if(bubbleNumber.getPostNum().equals("0")){
            binding.tvBubble1.setVisibility(View.GONE);
            flag_post = false;
        }else {
            binding.tvBubble1.setText(bubbleNumber.getPostNum());
            flag_post = true;
        }
        if(bubbleNumber.getSttentionNum().equals("0")){
            binding.tvBubble2.setVisibility(View.GONE);
            flag_attention = false;
        }else {
            binding.tvBubble2.setText(bubbleNumber.getSttentionNum());
            flag_attention = true;
        }
        if(bubbleNumber.getPrizeNum().equals("0")){
            binding.tvBubble3.setVisibility(View.GONE);
            flag_prize = false;
        }else{
            binding.tvBubble3.setText(bubbleNumber.getPrizeNum());
            flag_prize = true;
        }
        if(bubbleNumber.getOrderNum().equals("0")){
            binding.tvBubble4.setVisibility(View.GONE);
            flag_order = false;
        }else {
            binding.tvBubble4.setText(bubbleNumber.getOrderNum());
            flag_order = true;
        }

    }


    @Override
    public void onFailure() {
        Toast.makeText(Activity,"连接服务器失败",Toast.LENGTH_LONG).show();
    }

    private void disPlay(String str){
        Toast.makeText(Activity,str,Toast.LENGTH_LONG).show();
    }

}
